using Devil;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace GameToolkit
{
    public static class StringUtil
    {
        public static string Clamp(string str, int length)
        {
            if (str != null && str.Length > length)
                return $"{str.Substring(0, length)}...";
            else
                return str;
        }

        public static bool ParseArray(string str, ICollection<string> arr, char split = ',', char bracketl = '\0', char bracketr = '\0')
        {
            int p = 0;
            int offset = 0;
            StringBuilder buf = ParallelUtils.GetBuilder(str);
            if (bracketl == '\0' && bracketr == '\0')
            {
                for (int i = 0; i < buf.Length; i++)
                {
                    char c = buf[i];
                    if (c == split)
                    {
                        arr.Add(buf.ToString(offset, i - offset));
                        offset = i + 1;
                    }
                }
                if (offset < buf.Length)
                    arr.Add(buf.ToString(offset, buf.Length - offset));
                ParallelUtils.Release(buf);
                return true;
            }
            for (int i = 0; i < buf.Length; i++)
            {
                char c = buf[i];
                if (c == bracketl)
                {
                    p++;
                    if (p == 1)
                        offset = i + 1;
                }
                else if (c == bracketr)
                {
                    p--;
                    if (p == 0)
                    {
                        if (offset < i)
                            arr.Add(buf.ToString(offset, i - offset));
                        offset = i + 1;
                    }
                }
                else if (c == split && p == 1)
                {
                    arr.Add(buf.ToString(offset, i - offset));
                    offset = i + 1;
                }
                else if (p < 1)
                {
                    return false;
                }
            }
            ParallelUtils.Release(buf);
            return p == 0;
        }

        public static string[][] ParseMatrix(string s, char split = ',', char bracketl = '[', char bracketr = ']')
        {
            if (string.IsNullOrEmpty(s))
                return new string[0][];
            List<string> tmp = new List<string>();
            string[][] matrix;
            if (!ParseArray(s, tmp, split, bracketl, bracketr))
                return null;
            matrix = new string[tmp.Count][];
            List<string> tmp2 = new List<string>();
            for (int i = 0; i < matrix.Length; i++)
            {
                tmp2.Clear();
                if (!ParseArray(tmp[i], tmp2, split, bracketl, bracketr))
                    return null;
                var arr = new string[tmp2.Count];
                for (int j = 0; j < arr.Length; j++)
                {
                    arr[j] = tmp2[j];
                }
                matrix[i] = arr;
            }
            return matrix;
        }

        public static int[] ParseArray(string s, char split = ',', char bracketl = '\0', char bracketr = '\0')
        {
            List<string> arr = new List<string>(5);
            //arr.Clear();
            if (!ParseArray(s, arr, split, bracketl, bracketr))
            {
                return null;
            }
            var result = new int[arr.Count];
            int num;
            for (int i = 0; i < result.Length; i++)
            {
                if (!int.TryParse(arr[i].Trim(), out num))
                    return null;
                result[i] = num;
            }
            return result;
        }

        public static float[] ParseFloatArray(string s, char split = ',', char bracketl = '\0', char bracketr = '\0')
        {
            List<string> arr = new List<string>(5);
            if (!ParseArray(s, arr, split, bracketl, bracketr))
            {
                return null;
            }
            var result = new float[arr.Count];
            float num;
            for (int i = 0; i < result.Length; i++)
            {
                if (!Localization.TryParse(arr[i].Trim(), out num))
                    return null;
                result[i] = num;
            }
            return result;
        }


        public static string Vector4ToString(Vector4 v)
        {
            return string.Format(Localization.Universal, "({0},{1},{2},{3})", v.x, v.y, v.z, v.w);
        }

        public static string Vector3ToString(Vector3 v)
        {
            return string.Format(Localization.Universal, "({0},{1},{2})", v.x, v.y, v.z);
        }

        public static string Vector2ToString(Vector2 v)
        {
            return string.Format(Localization.Universal, "({0},{1})", v.x, v.y);
        }

        /// <summary>
        /// 将字符串(x,y,z)转换为 Vector3
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static Vector3 ParseVector3(string s)
        {
            string t = s.Trim();
            float[] arr = ParseFloatArray(t, ',', '(', ')');
            if (arr == null)
                return default(Vector3);
            Vector3 v = Vector3.zero;
            if (arr.Length > 0)
                v.x = arr[0];
            if (arr.Length > 1)
                v.y = arr[1];
            if (arr.Length > 2)
                v.z = arr[2];
            return v;
        }

        public static Vector2 ParseVector2(string s)
        {
            string t = s.Trim();
            float[] arr = ParseFloatArray(t, ',', '(', ')');
            if (arr == null)
                return default(Vector2);
            Vector2 v = Vector2.zero;
            if (arr.Length > 0)
                v.x = arr[0];
            if (arr.Length > 1)
                v.y = arr[1];
            return v;
        }
        public static Vector4 ParseVector4(string s)
        {
            string t = s.Trim();
            float[] arr = ParseFloatArray(t, ',', '(', ')');
            if (arr == null)
                return default(Vector4);
            Vector4 v = Vector4.zero;
            if (arr.Length > 0)
                v.x = arr[0];
            if (arr.Length > 1)
                v.y = arr[1];
            if (arr.Length > 2)
                v.z = arr[2];
            if (arr.Length > 3)
                v.w = arr[3];
            return v;
        }

        public static string RemoveSpaceBetweenChinese(string str)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            var content = ParallelUtils.GetBuilder(str);
            int charIndex = -1;
            for (int i = content.Length - 1; i >= 0; i--)
            {
                var c = content[i];
                if (charIndex > 0 && c != ' ' && c != '\t')
                {
                    content.Remove(i + 1, charIndex - i - 1);
                    charIndex = -1;
                }
                if (c >= '\u4e00' && c <= '\u9fa5')
                {
                    charIndex = i;
                }
            }
            return ParallelUtils.ReturnRelease(content);
        }
    }
}
